/**
 * 
 */
package org.teremail.message.builder;

import java.util.List;

import org.teremail.message.Entity;
import org.teremail.message.EntityVisitor;
import org.teremail.message.Multipart;
import org.teremail.message.MultipartEntity;
import org.teremail.message.MultipartMessage;
import org.teremail.message.SimpleEntity;
import org.teremail.message.SimpleMessage;

import com.google.common.collect.Lists;

class BuilderEntityVisitor implements EntityVisitor {

    private EntityBuilder builder = null;
    
    private List<EntityBuilder> getEntityBuilders(Multipart multipart) {
        List<EntityBuilder> partBuilders = Lists.newArrayList();
        for (Entity e : multipart) {
            BuilderEntityVisitor bev = new BuilderEntityVisitor();
            e.accept(bev);
            partBuilders.add(bev.getEntityBuilder());
        }
        return partBuilders;
    }
    
    public void visitMultipartEntity(MultipartEntity multipartEntity) {
        List<EntityBuilder> partBuilders = getEntityBuilders(multipartEntity);
        builder = new MultipartEntityBuilder(multipartEntity, partBuilders);
    }

    public void visitMultipartMessage(MultipartMessage multipartMessage) {
        List<EntityBuilder> partBuilders = getEntityBuilders(multipartMessage);
        builder = new MultipartMessageBuilder(multipartMessage, partBuilders);
    }

    public void visitSimpleEntity(SimpleEntity simpleEntity) {
        builder = new SimpleEntityBuilder(simpleEntity);
    }

    public void visitSimpleMessage(SimpleMessage simpleMessage) {
        builder = new SimpleMessageBuilder(simpleMessage);
    }
    
    public EntityBuilder getEntityBuilder() {
        return builder;
    }
    
}